package com.mcc.app.group.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mcc.app.group.domain.GroupClasses;
import com.mcc.app.group.mapper.GroupClassesMapper;
import com.mcc.app.group.service.IGroupClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 班次Service业务层处理
 *
 * @author gxy
 * @date 2021-07-08
 */
@Service
public class GroupClassesServiceImpl implements IGroupClassesService {

    @Autowired
    private GroupClassesMapper groupClassesMapper;

    /**
     * 查询班次
     *
     * @param classesId 班次ID
     * @return 班次
     */
    @Override
    public GroupClasses selectGroupClassesById(Long classesId) {
        return groupClassesMapper.selectById(classesId);
    }

    /**
     * 查询班次列表
     *
     * @param groupClasses 班次
     * @return 班次
     */
    @Override
    public List<GroupClasses> selectGroupClassesList(GroupClasses groupClasses) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(groupClasses)) {
            queryWrapper.like(ObjectUtil.isNotEmpty(groupClasses.getClassesName()), "classes_name", groupClasses.getClassesName());
            queryWrapper.eq(ObjectUtil.isNotEmpty(groupClasses.getCode()), "code", groupClasses.getCode());
        }
        queryWrapper.orderByAsc("sort");
        return groupClassesMapper.selectList(queryWrapper);
    }

    /**
     * 新增班次
     *
     * @param groupClasses 班次
     * @return 结果
     */
    @Override
    public int insertGroupClasses(GroupClasses groupClasses) {
        return groupClassesMapper.insert(groupClasses);
    }

    /**
     * 修改班次
     *
     * @param groupClasses 班次
     * @return 结果
     */
    @Override
    public int updateGroupClasses(GroupClasses groupClasses) {
        return groupClassesMapper.updateById(groupClasses);
    }

    /**
     * 删除班次
     *
     * @param classesId 班次ID
     * @return 结果
     */
    @Override
    public int deleteGroupClassesById(Long classesId) {
        return groupClassesMapper.deleteById(classesId);
    }

    /**
     * 批量删除班次
     *
     * @param classesIds 班次ID
     * @return 结果
     */
    @Override
    public int deleteGroupClassesByIds(Long[] classesIds) {
        if (classesIds.length == 0) return 0;
        return groupClassesMapper.deleteBatchIds(ListUtil.toList(classesIds));
    }

    @Override
    public GroupClasses selectGroupClassesByName(String classesName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("classes_name", classesName);
        queryWrapper.last("LIMIT 1");
        return groupClassesMapper.selectOne(queryWrapper);
    }
}
